<?php

namespace App\Console\Commands;

use App\Jobs\UpdateCartPricesJob;
use App\Models\Cart;
use Illuminate\Console\Command;

class TestQueue extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'queue:test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Test queue functionality by dispatching a job';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $this->info('Testing queue functionality...');
        
        // 获取第一个购物车
        $cart = Cart::first();
        
        if (!$cart) {
            $this->error('No carts found. Please create a cart first.');
            return Command::FAILURE;
        }
        
        $this->info("Dispatching UpdateCartPricesJob for cart ID: {$cart->id}");
        
        // 分发作业到队列
        dispatch(new UpdateCartPricesJob($cart));
        
        $this->info('Job dispatched successfully!');
        $this->info('Run "php artisan queue:work" to process the job.');
        
        return Command::SUCCESS;
    }
}